import 'package:flutter/material.dart';
import 'package:flutter_trip_demo/pages/search_page.dart';
import 'package:flutter_trip_demo/plugin/asr_manager.dart';


const double MIC_SIZES = 80;
class SpeaksPage extends StatefulWidget {
  @override
  _SpeaksPageState createState() => _SpeaksPageState();
}

class _SpeaksPageState extends State<SpeaksPage>
with SingleTickerProviderStateMixin{
   String Tips = '长按说话';
   String result = '';
   late Animation<double> animation;
   late AnimationController controller;
   @override
  void initState() {
     controller = AnimationController(vsync:this,duration:const Duration(milliseconds: 1000));
    animation = CurvedAnimation(parent: controller, curve: Curves.easeIn)
     ..addStatusListener((status) {
       if(status ==AnimationStatus.completed){
         controller.reverse();
       }else if(status == AnimationStatus.dismissed){
         controller.forward();
       }
    });
     super.initState();
  }
  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding: const EdgeInsets.all(30),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _top(),
              _bottom(),
            ],
          ),
        ),
      ),
    );
  }
   _bottom(){
   return FractionallySizedBox(
     widthFactor: 1,
     child: Stack(
       children: [
         GestureDetector(
           onTapDown: (e){
             _speaksstrat();
           },
           onTapUp: (e){
             _speaksstop();
           },
           child: Center(
             child: Column(
               children: [
                 Padding(padding: const EdgeInsets.all(10),
                 child: Text(
                   Tips,
                   style: const TextStyle(color: Colors.blue,fontSize: 12),
                 ),
                 ),
                 Stack(
                   children: [
                     const SizedBox(
                       height: MIC_SIZES,
                       width: MIC_SIZES,
                     ),
                     Center(
                       child: AnimatedMic(animation: animation),
                     )
                   ],
                 )
               ],
             ),
           ),
         ),
         Positioned(
             right: 0,
             bottom: 20,
             child: GestureDetector(
                onTap: (){
                  Navigator.pop(context);
                },
               child:
               const Icon(
                 Icons.close,
                 size: 30,
                 color: Colors.grey,
               ),
             )
         )
       ],
     ),
   );
  }
   _top(){
     return Column(
       children: [
         const Padding(
             padding: EdgeInsets.fromLTRB(0, 30, 0, 30),
           child:Text('你可以这样说',
           style: TextStyle(fontSize: 16,color: Colors.black54),),),
         const Text('故宫门票\n北京一日游\n迪士尼乐园',
         style: TextStyle(
           fontSize: 15,
           color: Colors.grey,
         )),
         Padding(padding: const EdgeInsets.all(20),
         child: Text(
           result,
           style: const TextStyle(color: Colors.blue),
         ),
         )
       ],
     );
   }
   _speaksstrat() {
     controller.forward();
     setState(() {
       Tips = '- 识别中 -';
     });
     AsrManager.start().then((text) {
       if (text?.isNotEmpty ?? true) {
         setState(() {
           result = text!;
         });
         Navigator.pop(context);
         Navigator.push(
             context,MaterialPageRoute(builder: (context)=>
             SearchPage(Keywords: result)));
         print("----------" + text!);
       }
     }).catchError((e) {
       print("----------" + e.toString());
     });
   }

   _speaksstop() {
     setState(() {
       Tips = '长按说话';
     });
     controller.reset();
     controller.stop();
     AsrManager.stop();
   }
}

class AnimatedMic extends AnimatedWidget{
  static final _opaciTween = Tween<double>(begin: 1,end: 0.5);
  static final _size = Tween<double>(begin: MIC_SIZES,end: MIC_SIZES-20);

  AnimatedMic({required Animation<double> animation}):super(listenable: animation);

  @override
  Widget build(BuildContext context) {
    final Animation<double> animation = listenable as Animation<double>;
    return Opacity(
        opacity:_opaciTween.evaluate(animation),
        child: Container(
          height: _size.evaluate(animation),
          width: _size.evaluate(animation),
          decoration: BoxDecoration(
            color: Colors.blue,
            borderRadius: BorderRadius.circular(MIC_SIZES/2),
          ),
          child: const Icon(Icons.mic,color: Colors.white,size: 30,),
        ),
    );
  }
}
